package com.bjpowernode.servlet;

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

public class AServlet extends GenericServlet {

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        //获取ServletContext对象
        ServletContext application = this.getServletContext();
        out.print("ServletContext对象是："+ application +"<br>");

        //根据上下文初始化参数
        Enumeration<String> initParamterNames = application.getInitParameterNames();
        while (initParamterNames.hasMoreElements()){
            String name= initParamterNames.nextElement();
            String value = application.getInitParameter(name);
            out.print(name + "="+ value + "<br>");
        }

        //获取context path(获取应用上下文的根)
        String contextPath = application.getContextPath();
        out.print(contextPath+"<br>");

        //准备数据
        User user = new User("jack","123");
        //乡ServletContext应用域中存储数据
        application.setAttribute("userOBJ",user);
        //取出来
        Object userOBJ =application.getAttribute("userOBJ");
        out.print(userOBJ+"<br>");
    }
}
